<?php
namespace Home\Controller;
use Think\Controller;
class SetController extends CommonController {
    //主页为 基本设置页面
    public function index(){
    	$id = $_SESSION['id'];
        $user = M('user')->where("id='{$id}'")->find();
        //var_dump($user);
        $this->assign('user',$user);
        $this->assign('title',"账号设置");
        $this->display();
    }

    //基本设置-> 确认修改我的资料时
    public function infoUpdate()
    {   
        $id=$_SESSION['id'];
        $email = I('post.email');

        //修改时检测邮箱格式不正确时返回提示
        $preg='/^[A-z0-9]\w*@[A-z0-9-]+\.[A-z]{2,}(\.[A-z{2,}])*$/';
        $c=preg_match($preg, $email);
        if (!$c) {
            $arr = array('error'=>1,'info'=>'邮箱格式不正确!!!',);
            echo json_encode($arr);exit;
        }

        //检测邮箱已存在时返回提示  邮箱不更改可以保存
        $sql = "select * from user where email='{$email}' and id!='{$id}'";
        $res = M('user')->query($sql);
        if ($res) {
            $arr = array('error'=>1,'info'=>'邮箱已存在!!!',);
            echo json_encode($arr);exit;
        }

        $nickname = I('post.nickname');

        //修改时检测昵称格式不正确时返回提示
        $preg='/^[\w|\W]{2,}$/';
        $c=preg_match($preg, $nickname);
        if (!$c) {
            $arr = array('error'=>1,'info'=>'昵称至少2个字符以上!!!',);
            echo json_encode($arr);exit;
        }

        //修改时检测昵称除了现在的昵称 而 已存在时返回提示
        $sql = "select * from user where nickname='{$nickname}' and id!='{$id}'";
        $res = M('user')->query($sql);
        if ($res) {
            $arr = array('error'=>1,'info'=>'昵称已存在!!!',);
            echo json_encode($arr);exit;
        }

        //数据正确时 执行修改 
        $data = I('post.');
        $res = M('user')->where("id='{$id}'")->save($data);

        if ($res) {
            //修改成功后继续存SESSION  昵称才能及时显示修改后的昵称
            $sql = "select * from user where id='{$id}'";
            $res = M()->query($sql);
            $res=current($res);

            $_SESSION['id']=$res['id'];
            $_SESSION['nickname']=$res['nickname']; 
            $_SESSION['face']=$res['face'];

            $arr = array('error'=>0,'info'=>'修改成功!!!',);
            echo json_encode($arr);exit;
        }else{
            //未作任何改动 时返回提示
            $arr = array('error'=>1,'info'=>'未做任何修改!!!',);
            echo json_encode($arr);exit;
        }


    }

    //基本设置-> 确认修改密码
    public function passUpdate()
    {
        // $nowpass = I('post.nowpass');    当前密码
        // $password = I('post.password');  新密码
        // $repass = I('post.repass');      确认密码
        $id = $_SESSION['id'];

        //检测新密码不符合要求时返回提示
        $password = I('post.password');
        $preg = '/^[\w|\W]{6,12}$/';
        $c = preg_match($preg, $password);
        if (!$c) {
            $arr = array('error'=>1,'info'=>'新密码必须6到12位!!!',);
            echo json_encode($arr);exit;
        }

        //检测两次密码不一致时返回提示
        $repass = I('post.repass');
        if ($password!=$repass) {
            $arr = array('error'=>1,'info'=>'两次密码不一致!!!',);
            echo json_encode($arr);exit;
        }

        //检测当前密码错误时返回提示   密码和id查不到时返回
        $nowpass = I('post.nowpass');
        $nowpass = md5($nowpass);
        $res = M('user')->where("password='{$nowpass}' and id='{$id}'")->find();
        if (!$res) {
            $arr = array('error'=>1,'info'=>'当前密码错误!!!',);
            echo json_encode($arr);exit;
        }

        //密码数据全正确时执行修改
        $password = md5($password);
        //$data['password'] = $password;
        //M('user')->where("id='{$id}'")-save($data);
        $sql = "update user set password='{$password}' where id='{$id}'";
              //UPDATE stu SET name = '大牛学院' WHERE id = 1 ;
        $res = M()->execute($sql);
        //var_dump($res); int1
        if ($res) {
            $arr = array('error'=>0,'info'=>'修改成功!!!',);
            echo json_encode($arr);exit;
        }else{
            $arr = array('error'=>1,'info'=>'修改失败!!!',);
            echo json_encode($arr);exit;
        }

    }

    //基本设置-> 处理头像 接到美图秀秀推过来的图片 进行处理
    public function upload()
    {
        // var_dump($_FILES);
        // <pre class='xdebug-var-dump' dir='ltr'>
        // <b>array</b> <i>(size=1)</i>
        //   'upload_file' <font color='#888a85'>=&gt;</font> 
        //     <b>array</b> <i>(size=5)</i>
        //       'name' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'1.jpg'</font> <i>(length=5)</i>
        //       'type' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'image/jpeg'</font> <i>(length=10)</i>
        //       'tmp_name' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'C:\wamp\tmp\php257D.tmp'</font> <i>(length=23)</i>
        //       'error' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>0</font>
        //       'size' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>11300</font>
        // </pre>
        
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件  //上传后的文件放在$info里
        $info   =   $upload->upload();
        // var_dump($info);
        // <pre class='xdebug-var-dump' dir='ltr'>
        // <b>array</b> <i>(size=1)</i>
        //   'upload_file' <font color='#888a85'>=&gt;</font> 
        //     <b>array</b> <i>(size=9)</i>
        //       'name' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'168.jpg'</font> <i>(length=7)</i>
        //       'type' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'image/jpeg'</font> <i>(length=10)</i>
        //       'size' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>9421</font>
        //       'key' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'upload_file'</font> <i>(length=11)</i>
        //       'ext' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'jpg'</font> <i>(length=3)</i>
        //       'md5' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'0cb9754f4aff63c61b7600b971950d27'</font> <i>(length=32)</i>
        //       'sha1' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'ae414571dbac185d5cd9488fb7aa47a2bfc91723'</font> <i>(length=40)</i>
        //       'savename' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'59aabfc848b66.jpg'</font> <i>(length=17)</i>
        //       'savepath' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'2017-09-02/'</font> <i>(length=11)</i>
        // </pre>上传成功！
        if(!$info) {// 上传错误提示错误信息
            echo $upload->getError();
        }else{// 上传成功
            $info = current($info);
            $path = "Uploads/".$info['savepath'].$info['savename'];
            //echo $path; //Uploads/2017-09-02/59aac4b26640b.jpg  $path是上传的新头像路径要存SESSION
            
            $id = $_SESSION['id'];
            $data['face'] = $path;
            $oldFace = $_SESSION['face']; //原头像路径 当上传新图像成功时 原头像要删除
            $res = M('user')->where("id='{$id}'")->save($data); //更新数据库的face路径 改为对应的$path
            if ($res) {
                $_SESSION['face'] = $path;
                if ($oldFace!='Public/images/168.jpeg') {
                    unlink($oldFace);
                    //rmdir(); 空目录未处理
                }
                echo '上传成功！图片保存位置:'.$path."..原图片路径为:".$oldFace;
            }else{
                echo 1;
            }
            
        }
    }





    //用户中心页面-> [我发的贴] 和 [我收藏的贴]
    public function userCore()
    {   
        $uid = $_SESSION['id'];
        //我发的贴  展示
        $sql = "select id,title,create_time from question where uid='{$uid}'";
        $myCards = M()->query($sql);  //$myCards  我发表过的帖子
        $this->assign('myCards',$myCards);

        //我收藏的贴 总条数及标题等展示
        $sql = "select t2.id,t2.title,t1.create_time from store t1 inner join question t2 on t1.qid=t2.id where t1.uid='{$uid}' order by create_time desc";
        $storeData = M()->query($sql); //$storeData 收藏的具体帖子 数据集合
        $this->assign('storeData',$storeData);

        $this->assign('title','用户中心');
        $this->display();
    }

    //发送邮件[若多处要执行该方法时可以放在公共function里面]  点击基本设置->我的资料->重新验证邮箱  时执行
    public function sendEmail()
    {   
        //调用公共function的verifyEmail()方法; [接收邮件的邮箱,标题,内容]
        verifyEmail("641166354@qq.com","测试邮箱的标题","测试邮箱的内容");
    }
    

}